package com.web;

import com.domain.Activity;
import com.service.activityService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 员工进行添加活动信息
 * 接收addActivity.jsp的页面数据
 * 接收为Activity类型的map函数
 * 提交到activityService的addActivity进行添加
 * 重定向到/findAllAct
 */
@WebServlet("/addActivity")
public class addActivityServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.解决乱码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //2.接收数据,
        Map<String, String[]> map = request.getParameterMap();
        //3.封装对象
        Activity activity = new Activity();
        try {
            BeanUtils.populate(activity,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("正在添加活动为" + activity );
        //4.进行添加
        Integer flag = new activityService().addActivity(activity);
        System.out.println(flag==1?"添加成功":"添加失败");
        //5.重定向到显示所有活动findAllAct
        response.sendRedirect(request.getContextPath()+"/findAllAct");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
